package com.yonyou.cyxdms.basedata.service.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yonyou.cloud.http.plus.domain.MessEvent;
import com.yonyou.cloud.http.plus.remote.IRemoteInvoke;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.basedata.service.constant.BasedataCommonConstants;
import com.yonyou.cyxdms.basedata.service.constant.BasedataDictConstants;
import com.yonyou.cyxdms.basedata.service.entity.basedata.SystemParamPO;
import com.yonyou.cyxdms.basedata.service.repository.basedata.SystemParamMapper;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;

import java.util.Map;

/**
 * 消息发送到MQ队列
 *
 * @author ysj
 * @since 10:32 2019-10-23
 */
public class MQUtils {

    private MQUtils() {
    }

    /**
     * 消息发送到mq
     *
     * @author ysj
     * @date 2019/10/23
     * @param jsonData
     * @param bizId
     */
    public static void sendMsg(Map<String,Object> jsonData, String bizId, SystemParamMapper systemParamMapper, IRemoteInvoke iRemoteInvoke){
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        LambdaQueryWrapper<SystemParamPO> query = new QueryWrapper<SystemParamPO>().lambda();
        query.eq(SystemParamPO::getAppId,loginInfoDto.getAppId());
        query.eq(SystemParamPO::getOwnerCode,loginInfoDto.getOwnerCode());
        query.eq(SystemParamPO::getParamCode,BasedataCommonConstants.SEND_MSG_PARAM_CODE);
        SystemParamPO systemParamPo = systemParamMapper.selectOne(query);

        //是否允许发送MQ消息
        if(!StringUtils.isNullOrEmpty(systemParamPo)&&BasedataDictConstants.PARAM_VALUE.equals(systemParamPo.getParamValue())){
            MessEvent messEvent = new MessEvent();
            messEvent.setUrl(BasedataCommonConstants.NOTIFY_URL + bizId);
            messEvent.setParam(jsonData);
            iRemoteInvoke.sendPostWithTransaction(messEvent);
        }
    }
}
